package cn.stu.projectsummit.likes.controller;

import cn.stu.projectsummit.base.response.JsonResult;
import cn.stu.projectsummit.base.response.JsonResult;
import cn.stu.projectsummit.likes.pojo.vo.LikeVO;
import cn.stu.projectsummit.likes.service.LikesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("v1/likes")
public class LikesController {
    @Autowired
    LikesService likesService;


    @GetMapping("/insert/{blogId}/{userId}/{like}")
    public JsonResult save(@PathVariable("blogId") Long blogId,@PathVariable("userId") Long userId ,@PathVariable("like")Integer like) {
        log.debug("点赞成功 blogId:={},userId:={},like:={}", blogId, userId, like);
        if (like == 1) {
            likesService.save(blogId, userId);
        } else {
            likesService.delete(blogId, userId);
        }
        return JsonResult.ok();
    }
    @GetMapping("/select/{blogId}")
    public JsonResult select(@PathVariable("blogId") Long blogId) {
        log.debug("查询点赞成功 blogId:={}", blogId);
        return JsonResult.ok(likesService.selectByBlogId(blogId));
    }


    @GetMapping("select")
    public JsonResult selectLikes() {
        List<LikeVO> list= likesService.select();
        return JsonResult.ok(list);
    }
    @PostMapping("delete/{likeId}")
    public JsonResult deleteLikes(@PathVariable Long likeId) {
        likesService.deleteLikes(likeId);
        return JsonResult.ok();
    }
}


